<?php
// +----------------------------------------------------------------------------
// | Tke团队版权所属 [三十年河东三十年河西,莫欺少年穷.!]
// +----------------------------------------------------------------------------
// | Copyright (c) 2014 http://www.itdaodan.com All rights reserved.
// +----------------------------------------------------------------------------
// | Author: sap<songaiping@hrbjjhh.com>
// +----------------------------------------------------------------------------

namespace Admin\Model;
use Admin\Model\BaseModel;
/**
 * @todo 店铺关系模型
 */

class ShopActivityModel extends BaseModel {

    /* 自动验证规则 */
    protected $_validate = array(
    	array('activity_id', 'isActivity', '必须选择活动', self::MUST_VALIDATE, 'function', self::MODEL_BOTH),
    	array('shop_id', 'isShop', '必须选择店铺', self::MUST_VALIDATE, 'function', self::MODEL_BOTH),
        array('booth_number', 'isOdd', '填写的数字必须是偶数 或者是1', self::EXISTS_VALIDATE , 'callback', self::MODEL_BOTH),
        array('cooperation_year','0,1', '请正确选择是否全年合作', self::EXISTS_VALIDATE , 'in', self::MODEL_BOTH),
        array('code_id','isNumeric', '请扫描参展商二维码', self::EXISTS_VALIDATE , 'function', self::MODEL_BOTH),
    );

    
    /**
     * 验证是否为偶数
     * @param int $num
     * @return boolean
     */
    function isOdd($num){
        $num = (int)$num;
        if($num == 1) return true;
        if($num%2 == 0) return true;
        return false;
    }
    /**
     * 新增店铺关系
     * @return boolean fasle 失败 ， int  成功 返回店铺ID
     * @author sap
     * @since 2015-8-17
     */
    public function update(){
        
        /* 获取数据对象 */
    	$data = $this->create($_POST);
        if(empty($data)){
            return false;
        }
        
        if((int)$data['shop_activity_id'] == 0){
            //判断关系是否存在，存在则报错
            $map_exit['activity_id'] = $data['activity_id'];
            $map_exit['shop_id'] = $data['shop_id'];
            if($this->where($map_exit)->select()){
            	$this->error = '该活动下已经存在这个店铺';
            	return false;
            }
        }
        //输入二维码时，验证二维码的绑定规则
        $code_id = (int)$data['code_id'];
        if($code_id>0){
            $activity_id = (int)$data['activity_id'];
            $shop_activity_id = (array)(int)$data['shop_activity_id'];

            $maps['activity_id'] = $activity_id;
            $maps['code_id'] = $code_id;
            $maps['shop_activity_id'] = array('notin',$shop_activity_id);
            $count = $this->where($maps)->count();
            if($count>0){
                $this->error = '此二维码已经被绑定过,请换一张二维码';
                return false;
            }
        }
        
        /* 添加或新增活动配置 */
        if(empty($data[$this->getPk()])){ //新增数据
            $id = $this->add(); //添加活动配置
            if(!$id){
                $this->error = '新增店铺出错！';
                return false;
            }
        } else { //更新数据
            $id = $this->save(); //更新活动配置
            if(false === $id){
                $this->error = '更新店铺出错！';
                return false;
            }
        }
        //内容添加或更新完成
        return $id;
    }
    
    /**
     * @todo  查看店铺是否在活动下
     * @param int activity_id 活动id
     * @param int shop_id 店铺id
     * @author PHP@妖孽 <dino_ma@163.com>
     */
    public function shopIsInActivity($activity_id=0,$shop_id=0){
    	$activity_id = (int)I('activity_id');
    	$shop_id = (int)I('shop_id');
    	if($activity_id ==0){
    		$this->error = '活动id有误';
    		return false;
    	}
    	if($shop_id ==0){
    		$this->error = '店铺Id出错！';
    		return false;
    	}
    	return $this->where(array('activity_id'=>$activity_id,'shop_id'=>$shop_id))->find();
    }
    
    /**
     * @todo 删除店铺和活动关系
     * @author sap
     */
    public function shopActivityDel($map = array()){
    	$map = (array)$map;
    	if(empty($map)){
    		$this-> error= '查询条件不正确';
    		return false;
    	}
    	if($this->where($map)->delete() === false){
    		$this->error = '删除失败！';
    		return false;
    	}
    	return true;
    }
    
    /**
     * 绑定二维码
     * @param int $code_id 二维码id
     * @param int $activity_id 活动id
     * @param int $shop_activity_id 参展店铺id
     * @author fzh
     */
    public function updateCode($code_id,$activity_id,$shop_activity_id){
        $code_id = (int)$code_id;
        $activity_id = (int)$activity_id;
        $shop_activity_id = (int)$shop_activity_id;
        if($code_id == 0){
            $this->error = '请扫描二维码';
            return false;
        }
        if($activity_id == 0){
            $this->error = '活动id错误';
            return false;
        }
        
        $maps['activity_id'] = $activity_id;
        $maps['code_id'] = $code_id;
        $maps['shop_activity_id'] = array('notin',(array)$shop_activity_id);
        $count = $this->where($maps)->count();
        if($count>0){
            $this->error = '此二维码已经被绑定过,请换一张二维码';
            return false;
        }
        return true;
    }
    
    /**
     * 
     * 二维码签到
     * @param int $activity_id 活动id
     * @param int $code_id 二维码
     * @author fzh
     */
    public function activitySginCode($activity_id,$code_id){
        $activity_id = (int)$activity_id;
        $code_id = (int)$code_id;
        if($activity_id == 0){
            $this->error = '活动id错误';
            return false;
        }
        if($code_id == 0){
            $this->error = '请正确的扫描二维码';
            return false;
        }
        $map['activity_id'] = $activity_id;
        $map['code_id'] = $code_id;
        $info = M('shop_activity')->where($map)->find();
        if(empty($info)){
            $this->error = '此二维码在此次活动中没有绑定商户';
            return false;
        }
        if($info['is_sign'] != 0){
            $this->error = '此二维码已经签到';
            return false;
        }
        $data['is_sign'] = 1;
        $data['create_time'] = NOW_TIME;
        $id = $this->where($map)->save($data);
        if($id > 0){
            return $id;
        }else{
            $this->error = '签到失败';
            return false;
        }
    }
    
    public function getPk() {
        return 'shop_activity_id';
    }
}
